package somar_client;

import java.net.Socket;
import java.util.Hashtable;

import org.osgi.framework.BundleActivator;
import org.osgi.framework.BundleContext;

import services.RobotService;
import services.RobotServiceImpl;
import ch.ethz.iks.r_osgi.RemoteOSGiService;

public class Activator implements BundleActivator {

	private Socket cSocket;
	
	private String ip = "203.253.23.42";
	private int port = 1234;
	
	/*
	 * (non-Javadoc)
	 * @see org.osgi.framework.BundleActivator#start(org.osgi.framework.BundleContext)
	 */
	public void start(BundleContext context) throws Exception {
		System.out.println("[SOMAR Client] SOMAR Client has runned.");
		
		Hashtable<String, Boolean> property = new Hashtable<String, Boolean>();
		property.put(RemoteOSGiService.R_OSGi_REGISTRATION, Boolean.TRUE);
		context.registerService(RobotService.class.getName(), new RobotServiceImpl(), property);
		
		cSocket = new Socket(ip, port);
	}

	/*
	 * (non-Javadoc)
	 * @see org.osgi.framework.BundleActivator#stop(org.osgi.framework.BundleContext)
	 */
	public void stop(BundleContext context) throws Exception {

		if(cSocket.isConnected() == true) {
			this.cSocket.close();
		}
		System.out.println("[SOMAR Client] SOMAR Client has finished.");
	}

}
